<?php
namespace Home\Model;
use Think\Model;
class RoleModel extends Model 
{
	protected $insertFields = array('role_name');
	protected $updateFields = array('id','role_name');
	protected $_validate = array(
		array('role_name', 'require', '角色名称不能为空！', 1, 'regex', 3),
		array('role_name', '1,30', '角色名称的值最长不能超过 30 个字符！', 1, 'length', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($role_name = I('get.role_name'))
			$where['role_name'] = array('like', "%$role_name%");
		/************************************* 翻页 ****************************************/
		$count = $this->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->field('a.*,group_concat(c.pri_name) pri_name')->alias('a')->join('left join crm_role_privilege b on a.id=b.role_id left join crm_privilege c on b.pri_id=c.id')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
	}
	// 添加后
	protected function _after_insert(&$data, $option)
	{
            $priID=I('post.pri_id');
            if($priID){
                $rpModel=M('RolePrivilege');
                foreach ($priID as $v){
                    $rpModel->add(array(
                        'role_id' => $data['id'],
                        'pri_id' => $v,
                    ));
                }
            }
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
            $arModel=M('RolePrivilege');
            $arModel->where(array('role_id'=>array('eq',$option['where']['id'])))->delete();
            $priId=I('post.pri_id');
            if($priId){
                $model = M('RolePrivilege');
                foreach ($priId as $k => $v) {
                $model->add(array(
                    'pri_id' => $v,
                    'role_id' => $option['where']['id'],
                ));
                }
            }
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}